home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / modules / contact.module < prev    next >
Encoding:
Text File  |  2005-04-11  |  5.1 KB  |  138 lines

  1. <?php
  2. // $Id: contact.module,v 1.6.2.1 2005/04/11 11:06:13 unconed Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Enables the use of personal contact forms.
  7.  */
  8.  
  9. // Users are not allowed to send more than x mails/hour:
  10. define('CONTACT_HOURLY_THRESHOLD', 3);
  11.  
  12. /**
  13.  * Implementation of hook_help().
  14.  */
  15. function contact_help($section) {
  16.   switch ($section) {
  17.     case 'admin/modules#description':
  18.       return t('Enables the use of personal contact forms.');
  19.   }
  20. }
  21.  
  22. /**
  23.  * Implementation of hook_menu().
  24.  */
  25. function contact_menu($may_cache) {
  26.   global $user;
  27.   $items = array();
  28.  
  29.   if (!$may_cache) {
  30.     if (arg(0) == 'user' && is_numeric(arg(1))) {
  31.       $items[] = array('path' => "user/". arg(1) ."/contact", 'title' => t('contact'),
  32.         'callback' => 'contact_mail_user', 'type' => MENU_LOCAL_TASK, 'weight' => 2);
  33.     }
  34.   }
  35.  
  36.   return $items;
  37. }
  38.  
  39. /**
  40.  * Implementation of hook_user().
  41.  *
  42.  * Provides signature customization for the user's comments.
  43.  */
  44. function contact_user($type, $edit, &$user, $category = NULL) {
  45.   if ($type == 'form' && $category == 'account') {
  46.     return array(array('title' => t('Contact settings'), 'data' => form_checkbox(t('Personal contact form'), 'contact', 1, $edit['contact'], t('Allow other users to contact you by e-mail via <a href="%url">your personal contact form</a>. Note that your e-mail address is not made public and that privileged users such as site administrators are able to contact you even if you choose not to enable this feature.', array('%url' => "user/$user->uid/contact"))), 'weight' => 2));
  47.   }
  48.   if ($type == 'validate') {
  49.     return array('contact' => $edit['contact']);
  50.   }
  51. }
  52.  
  53. function contact_mail_user() {
  54.   global $user;
  55.  
  56.   if ($account = user_load(array('uid' => arg(1), 'status' => 1))) {
  57.     if (!$account->contact && !user_access('administer users')) {
  58.       $output = t('%name is not accepting e-mails.', array('%name' => $account->name));
  59.     }
  60.     else if (!$user->uid) {
  61.       $output = t('Please <a href="%login">login</a> or <a href="%register">register</a> to send %name a message.', array('%login' => url('user/login'), '%register' => url('user/register'), '%name' => $account->name));
  62.     }
  63.     else if (!valid_email_address($user->mail)) {
  64.       $output = t('You need to provide a valid e-mail address to contact other users. Please edit your <a href="%url">user information</a>.', array('%url' => url("user/$user->uid/edit")));
  65.     }
  66.     else if (!flood_is_allowed('contact', CONTACT_HOURLY_THRESHOLD)) {
  67.       $output = t("You can't contact more than %number users per hour. Please try again later.", array('%number' => CONTACT_HOURLY_THRESHOLD));
  68.     }
  69.     else {
  70.       $edit = $_POST['edit'];
  71.  
  72.       if ($edit) {
  73.         // Validate the message:
  74.         if (!$edit['message']) {
  75.           form_set_error('message', t('You must enter a message.'));
  76.         }
  77.         if (!$edit['subject']) {
  78.           form_set_error('subject', t('You must enter a subject.'));
  79.         }
  80.  
  81.         if (!form_get_errors()) {
  82.           // Compose the body:
  83.           $message[] = "$account->name,";
  84.           $message[] = t("%name (%name-url) has sent you a message via your contact form (%form-url) at %site.", array('%name' => $user->name, '%name-url' => url("user/$user->uid", NULL, NULL, TRUE), '%form-url' => url($_GET['q'], NULL, NULL, TRUE), '%site' => variable_get('site_name', 'drupal')));
  85.           $message[] = t("If you don't want to receive such e-mails, you can change your settings at %url.", array('%url' => url("user/$account->uid", NULL, NULL, TRUE)));
  86.           $message[] = t('Message:');
  87.           $message[] = $edit['message'];
  88.  
  89.           // Tidy up the body:
  90.           foreach ($message as $key => $value) {
  91.             $message[$key] = wordwrap($value);
  92.           }
  93.  
  94.           // Prepare all fields:
  95.           $to = $account->mail;
  96.           $from = $user->mail;
  97.  
  98.           // Format the subject:
  99.           $subject = '['. variable_get('site_name', 'drupal') .'] '. $edit['subject'];
  100.  
  101.           // Prepare the body:
  102.           $body = implode("\n\n", $message);
  103.  
  104.           // Send the e-mail:
  105.           user_mail($to, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
  106.  
  107.           // Log the operation:
  108.           flood_register_event('contact');
  109.           watchdog('mail', t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name)));
  110.  
  111.           // Set a status message:
  112.           drupal_set_message(t('Your message has been sent.'));
  113.  
  114.           // Jump to the user's profile page:
  115.           drupal_goto("user/$account->uid");
  116.         }
  117.       }
  118.       else {
  119.         $edit['mail'] = $user->mail;
  120.       }
  121.  
  122.       $output  = form_item(t('From'), $user->name .' <'. $user->mail .'>');
  123.       $output .= form_item(t('To'), $account->name);
  124.       $output .= form_textfield(t('Subject'), 'subject', $edit['subject'], 50, 50, NULL, NULL, TRUE);
  125.       $output .= form_textarea(t('Message'), 'message', $edit['message'], 70, 8, NULL, NULL, TRUE);
  126.       $output .= form_submit(t('Send e-mail'));
  127.       $output  = form($output);
  128.     }
  129.  
  130.     print theme('page', $output, $account->name);
  131.   }
  132.   else {
  133.     drupal_not_found();
  134.   }
  135. }
  136.  
  137. ?>
  138.